package com.jiangli.config;

import com.jiangli.interceptor.JwtTokenAdminInterceptor;
import com.jiangli.interceptor.JwtTokenUserInterceptor;
import com.jiangli.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;


    /**
     * 注册自定义拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
       log.info("开始注册自定义拦截器...");

       registry.addInterceptor(jwtTokenAdminInterceptor)
               .excludePathPatterns("/admin/admin/login")
               .addPathPatterns("/admin/**");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/user/wxlogin")
                .excludePathPatterns("/user/dish/random")
                .excludePathPatterns("/user/dish/list")
                .excludePathPatterns("/user/merchant/recommend")
                .excludePathPatterns("/user/dish/rank")
                .excludePathPatterns("/user/dish/calorieRank")
                .excludePathPatterns("/user/dish/getDetail")
                .excludePathPatterns("/user/dish/search")
                .excludePathPatterns("/user/merchant/search")
                .excludePathPatterns("/user/ingredient/search")
                .excludePathPatterns("/user/dish/rate")
                .excludePathPatterns("/user/actions")
                .addPathPatterns("/user/dish/recommend")
                .addPathPatterns("/user/merchant/rank");
    }

    /**
     * Knife4j接口文档配置
     */
    @Bean
    public Docket docket1() {
        // 管理端接口配置...
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("管理端接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jiangli.controller.admin"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket docket2() {
        // 用户端接口配置...
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jiangli.controller.user"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 设置静态资源映射 - 关键修复点
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 1. 配置Knife4j文档资源
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/v2/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");

        // 2. 配置项目静态资源（可选，根据你的项目结构调整）
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/public/**")
                .addResourceLocations("classpath:/public/");

        // 3. 配置favicon.ico（解决警告）
        registry.addResourceHandler("/favicon.ico")
                .addResourceLocations("classpath:/static/"); // 确保static目录下有favicon.ico

        // 4. 保留父类的默认配置（可选，但建议添加）
        super.addResourceHandlers(registry);
    }

    /**
     * 扩展消息转换器
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // JSON消息转换器配置...
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0, converter);
    }
}